//
// Created by Jeffrey Wei on 2022/11/2.
// 请求扩展,基础请求为rxRequest,会返回一个ApiResponse
// 若定制过解析器dataParserType,还可调用rxRequestModel和rxRequestModelArray

import Foundation
import Moya
import RxSwift

public extension NetworkTargetType {
    ///
    /// 常规请求
    /// - Returns:返回为结果类型NetworkResponse
    func rxRequest() -> Single<NetworkResponse> {
        let result = type(of: self).provider.rx.request(self)
                .mapJSON().map { $0 as? [String: Any] ?? [:] }
                .map { self.responseType.init(json: $0)! as NetworkResponse }
                .catch { Single.error($0.asNetworkError ?? $0) }
        return result
    }


    /// 取消所有请求
    static func cancelAllRequests() {
        provider.session.cancelAllRequests()
    }
}
